<p>The instance passed to the <a
href="https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/synclock-statement"><code>SyncLock</code> statement</a>
should be a dedicated private field.</p>
<h2>Why is this an issue?</h2>
<p>If the instance representing an exclusively acquired lock is publicly accessible, another thread in another part of the program could accidentally
attempt to acquire the same lock. This increases the likelihood of <a href="https://en.wikipedia.org/wiki/Deadlock">deadlocks</a>.</p>
<p>For example, a <code>string</code> should never be used for locking. When a <code>string</code> is <a
href="https://en.wikipedia.org/wiki/Interning_(computer_science)">interned</a> by the runtime, it can be shared by multiple threads, breaking the
locking mechanism.</p>
<p>Instead, a dedicated private <a href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.lock?view=net-9.0"><code>Lock</code></a> object
instance (or <code>object</code> instance, for frameworks before .Net 9) should be used for locking. This minimizes access to the lock instance and
therefore prevents accidential lock sharing.</p>
<p>The following objects are considered potentially prone to accidental lock sharing:</p>
<ul>
  <li> a reference to <a
  href="https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/program-structure/me-my-mybase-and-myclass#me">Me</a>: if the instance
  is publicly accessible, the lock might be shared </li>
  <li> a <a href="https://learn.microsoft.com/en-us/dotnet/api/system.type">Type</a> object: if the type class is publicly accessible, the lock might
  be shared </li>
  <li> a <a href="https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/string-data-type">String</a> literal or
  instance: if any other part of the program uses the same string, the lock is shared because of interning </li>
</ul>
<h2>How to fix it</h2>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
Public Sub MyLockingMethod()
    SyncLock Me 'Noncompliant
        ' ...
    End SyncLock
End Sub
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
Private lockObj As New Object()
Public Sub MyLockingMethod()
    SyncLock lockObj
        ' ...
    End SyncLock
End Sub
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
  <li> Wikipedia - <a href="https://en.wikipedia.org/wiki/Thread_(computing)">Thread</a> </li>
  <li> Wikipedia - <a href="https://en.wikipedia.org/wiki/Lock_(computer_science)">Locking</a> </li>
  <li> Wikipedia - <a href="https://en.wikipedia.org/wiki/Deadlock">Deadlock</a> </li>
  <li> Wikipedia - <a href="https://en.wikipedia.org/wiki/Interning_(computer_science)">Interning</a> </li>
  <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.string.intern#remarks">String interning by the runtime</a> </li>
  <li> Microsoft Learn - <a href="https://docs.microsoft.com/en-us/dotnet/standard/threading/managed-threading-best-practices">Managed Threading Best
  Practices</a> </li>
  <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/synclock-statement">SyncLock
  Statement</a> </li>
</ul>

